package com.dragon.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Chris Chan
 * Create on 2022/1/4 23:02
 * Use for: 驼峰工具
 * Explain:
 */
public class CamelUtil {
    /**
     * 驼峰转下划线模式
     *
     * @param str
     * @return
     */
    public static String camelToUnderLineMode(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        String s = sb.toString();
        while (s.startsWith("_")) {
            s = s.substring(1);
        }
        return s;
    }
}
